import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import moment from 'moment';
import { deptSimple } from '/@/api/backs/workFlow/form';
import { DICT_TYPE, getDictDatas } from '/@/utils/dict';

export const columns: BasicColumn[] = [
  {
    title: '排序',
    dataIndex: 'sort',
  },
  {
    title: '角色名称',
    dataIndex: 'name',
  },
  {
    title: '角色标识',
    dataIndex: 'code',
  },
  {
    title: '备注',
    dataIndex: 'remark',
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    customRender: ({ record }) => {
      const time = record.createTime;
      return time ? moment(time).format('YYYY-MM-DD') : '-';
    },
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'name',
    label: '角色名称',
    component: 'Input',
    colProps: { span: 8 },
    componentProps: ({ tableAction }) => {
      return {
        placeholder: '请输入角色名称',
        onPressEnter: () => {
          const { reload } = tableAction;
          reload();
        },
      };
    },
  },
  {
    field: 'code',
    label: '角色标识',
    component: 'Input',
    colProps: { span: 8 },
    componentProps: ({ tableAction }) => {
      return {
        placeholder: '请输入角色标识',
        onPressEnter: () => {
          const { reload } = tableAction;
          reload();
        },
      };
    },
  },
  {
    field: 'createTime',
    label: '创建时间',
    component: 'RangePicker',
    colProps: { span: 8 },
    componentProps: {
      valueFormat: 'X',
    },
  },
];
//编辑
const isShow = (type: string) => type === 'power';
const isData = (type: string) => type === 'dataPower';
const isAddEdit = (type: string) => type === 'add' || type === 'edit';

const isAll = (type: string) => type === 'dataPower' || type === 'power';

export const formSchema: FormSchema[] = [
  {
    field: 'name',
    label: '角色名称',
    component: 'Input',
    required: true,
    componentProps: {
      placeholder: '请输入角色名称',
    },
    dynamicDisabled: ({ values }) => isAll(values.propsType),
  },
  {
    field: 'code',
    label: '角色标识',
    component: 'Input',
    required: true,
    componentProps: {
      placeholder: '请输入角色标识',
    },
    dynamicDisabled: ({ values }) => isAll(values.propsType),
  },
  {
    field: 'sort',
    label: '序号',
    component: 'Input',
    required: true,
    defaultValue: '255',
    componentProps: {
      placeholder: '请输入角色序号',
    },
    ifShow: ({ values }) => !isAll(values.propsType),
  },
  {
    field: 'remark',
    label: '备注',
    component: 'InputTextArea',
    componentProps: {
      placeholder: '请输入备注',
    },
    ifShow: ({ values }) => !isAll(values.propsType),
  },
  {
    field: 'dataScope',
    label: '数据权限',
    component: 'Select',
    colProps: { span: 8 },
    required: true,
    componentProps: (values) => {
      return {
        options: getDictDatas(DICT_TYPE.SYSTEM_DATA_SCOPE),
        placeholder: '请选择数据权限',
        onChange: (e: any) => {
          values.formModel.dataScopeDeptIds = [];
        },
      };
    },
    ifShow: ({ values }) => {
      return !isShow(values.propsType)
    },
  },
  {
    field: 'dataScopeDeptIds',
    label: '指定部门',
    required: true,
    component: 'ApiTreeSelect',
    componentProps: {
      placeholder: '请选择指定部门',
      api: deptSimple,
      labelField: 'name',
      valueField: 'id',
      treeCheckable: true,
      replaceFields: {
        children: 'children',
        title: 'name',
        key: 'id',
        value: 'id',
      },
    },
    ifShow: ({ values }) => {
      return !isShow(values.propsType) && values.dataScope == '2';
    },
  },
  {
    field: 'menuIds',
    label: '菜单权限',
    component: 'Input',
    slot: 'productValueDom',
    colProps: { lg: 24, md: 24 },
    ifShow: ({ values }) => !isData(values.propsType),
  },
];
